#!/usr/bin/perl

use strict;

use lib qw( lib t/lib );
use SQLiteTest;
use DataValidatorSchemaLoader;
use Data::Dumper;
use Test::More tests => 3;
use Scalar::Util qw/ blessed /;

my $schema = SQLiteTest::get_test_schema( "DataValidatorSchemaLoader" );
my $id = 10;

ok( test_create(), "Test create with native validation" );
ok( test_update(), "Test update with native validation" );
ok( test_update_all(), "Test update_all with native validation" );

sub test_create {
    eval {
        my $test = $schema->resultset( 'Native' )->create( {
            id       => $id++,
            col_int  => 1,
            col_real => 0.1,
            col_char => 'some name'
        } );
    };
    return 0 if $@;
    
    # test int failure
    eval {
        my $test = $schema->resultset( 'Native' )->create( {
            id       => $id++,
            col_int  => 0.1,
            col_real => 0.1,
            col_char => 'some name'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_int' ) ) {
        return 0;
    }
    
    # test real failure
    eval {
        my $test = $schema->resultset( 'Native' )->create( {
            id       => $id++,
            col_int  => 1,
            col_real => '0.1.1',
            col_char => 'some name'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_real' ) ) {
        return 0;
    }
    
    # test char failure
    eval {
        my $test = $schema->resultset( 'Native' )->create( {
            id       => $id++,
            col_int  => 1,
            col_real => 0.1,
            col_char => 'some name wich is far to long'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_char' ) ) {
        return 0;
    }
    
    return 1;
}

sub test_update {
    eval {
        my $test = $schema->resultset( 'Native' )->search->first->update( {
            id       => $id++,
            col_int  => 1,
            col_real => 0.1,
            col_char => 'some name'
        } );
    };
    return 0 if $@;
    
    # test int failure
    eval {
        my $test = $schema->resultset( 'Native' )->search->first->update( {
            id       => $id++,
            col_int  => 0.1,
            col_real => 0.1,
            col_char => 'some name'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_int' ) ) {
        return 0;
    }
    
    # test real failure
    eval {
        my $test = $schema->resultset( 'Native' )->search->first->update( {
            id       => $id++,
            col_int  => 1,
            col_real => '0.1.1',
            col_char => 'some name'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_real' ) ) {
        return 0;
    }
    
    # test char failure
    eval {
        my $test = $schema->resultset( 'Native' )->search->first->update( {
            id       => $id++,
            col_int  => 1,
            col_real => 0.1,
            col_char => 'some name wich is far to long'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_char' ) ) {
        return 0;
    }
    
    return 1;
}


sub test_update_all {
    eval {
        my $test = $schema->resultset( 'Native' )->search->update_all( {
            col_int  => 1,
            col_real => 0.1,
            col_char => 'some name'
        } );
    };
    return 0 if $@;
    
    # test int failure
    eval {
        my $test = $schema->resultset( 'Native' )->search->update_all( {
            col_int  => 0.1,
            col_real => 0.1,
            col_char => 'some name'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_int' ) ) {
        return 0;
    }
    
    # test real failure
    eval {
        my $test = $schema->resultset( 'Native' )->search->update_all( {
            col_int  => 1,
            col_real => '0.1.1',
            col_char => 'some name'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_real' ) ) {
        return 0;
    }
    
    # test char failure
    eval {
        my $test = $schema->resultset( 'Native' )->search->update_all( {
            col_int  => 1,
            col_real => 0.1,
            col_char => 'some name wich is far to long'
        } );
    };
    unless ( $@ && blessed( $@ ) && $@->has_error( 'col_char' ) ) {
        return 0;
    }
    
    return 1;
}

1;
